//
//  AutoScrollController.m
//  滚动的小视图
//
//  Created by 张令林 on 16/9/26.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "AutoScrollController.h"

static NSString *autoScrollControllerCell = @"autoScrollControllerCell";

@interface AutoScrollController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UITableViewCell *cell;

@end

@implementation AutoScrollController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建定时器
    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(setOffSet) userInfo:nil repeats:YES];
    //定时器立即执行一次
    //[timer fire];
    //开启定时器
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    self.timer = timer;
    

    //设置tableView的数据源和代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 30;//默认行高为30
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.pagingEnabled = YES;
    
    [self scrollViewDidScroll:self.tableView];
    //调用初始化方法
    [self setUpUI];
    
}
#pragma mark 初始化方法
- (void)setUpUI
{
    
}


#pragma mark 判断tableView的偏移量,定时器执行的方法
- (void)setOffSet
{
    CGFloat currentOffsetY = self.tableView.contentOffset.y;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.contentOffset = CGPointMake(0, currentOffsetY-self.tableView.rowHeight);
    }];
    
    
}


#pragma mark - 在监听滑动结束的方法内判断偏移量
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //NSLog(@"%f-------",scrollView.contentOffset.y);
    
    CGFloat currentOffsetY = scrollView.contentOffset.y;
    //计算最后的偏移量
    CGFloat lastOffSetY = (self.tableView.rowHeight)*(self.array.count-1);
    if (currentOffsetY == 0 || currentOffsetY >= lastOffSetY)
    {
        //求出一半的偏移位置
        CGFloat centerOffSetY = (self.tableView.rowHeight)*(self.array.count*0.5);
        scrollView.contentOffset = CGPointMake(0,centerOffSetY-self.tableView.rowHeight);
    }
}
#pragma mark - 定时器的销毁
-(void)removeFromParentViewController
{
    [super removeFromParentViewController];
    [self.timer invalidate];
}

#pragma mark - tableView的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"-------%zd",self.array.count);
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"autoScrollControllerCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        [self setCell:cell];
    }
    cell.textLabel.text = self.array[indexPath.row];
    [cell.textLabel setTextColor:[UIColor darkGrayColor]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];

    return cell;
}

#pragma mark - 重写Array的set方法,设置器数值
-(void)setArray:(NSArray *)array
{
    
    if (_array == nil)
    {
        NSMutableArray *tempArray = [NSMutableArray array];
        for (int i = 0; i < 2; i++)
        {
            for (id obj in array)
            {
                [tempArray addObject:obj];
            }
        }
        _array = tempArray.copy;
    }else
    {
        _array = array;
    }
    
}

#pragma mark 设置cell
- (void)setCell:(UITableViewCell *)cell
{
    
    UIView *lineView = [[UIView alloc] init];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jingxuan"]];
    UILabel *title = [[UILabel alloc] init];
    title.text = @"123";
    [title setFont:[UIFont systemFontOfSize:16]];
    [title setTextColor:[UIColor darkGrayColor]];
    
    //添加到cell中
    [cell addSubview:lineView];
    [cell addSubview:imageView];
    [cell addSubview:title];
    
    //设置布局
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.bottom.offset(-5);
        make.width.offset(1);
        make.left.offset(0);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.bottom.offset(-5);
        make.width.offset(60);
        make.left.equalTo(lineView.mas_right).offset(5);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.bottom.offset(-5);
        make.left.offset(5);
        make.right.offset(-5);
    }];
    
    
}
@end
